home *** CD-ROM | disk | FTP | other *** search
/ Aminet 43 / Aminet 43 (2001)(GTI - Schatztruhe)[!][Jun 2001].iso / Aminet / comm / tcp / Amster-source.lha / Amster_Install / Source / msg.c < prev    next >
C/C++ Source or Header  |  2001-03-14  |  10KB  |  349 lines

  1. /*
  2. ** Amster - Messenger
  3. ** Copyright © 1999-2000 by Gürer Özen
  4. ** Copyright © 2000-2001 by Jacob Laursen
  5. **
  6. ** This program is free software; you can redistribute it and/or modify
  7. ** it under the terms of the GNU General Public License as published by
  8. ** the Free Software Foundation; either version 2 of the License, or
  9. ** (at your option) any later version.
  10. **
  11. ** This program is distributed in the hope that it will be useful,
  12. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. ** GNU General Public License for more details.
  15. **
  16. ** You should have received a copy of the GNU General Public License
  17. ** along with this program; if not, write to the Free Software
  18. ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19. */
  20.  
  21. #include "amster.h"
  22.  
  23. #include <proto/dos.h>
  24. #include <proto/utility.h>
  25. #include <MUI/NListview_mcc.h>
  26. #include <MUI/textinput_mcc.h>
  27.  
  28. #include "chatline.h"
  29. #include "amster_Cat.h"
  30.  
  31. ULONG msg_new(struct IClass *cl, Object *obj, struct opSet *msg);
  32. MUI_LIST_DISP_DECL(MessageDisplay, struct ChatMessage *m);
  33. void msg_say(struct msgdata *data);
  34. void msg_whois(struct msgdata *data, char *info);
  35. void msg_whowas(struct msgdata *data, char *user, char *level, u_long lastseen);
  36. void InsertMsgEntry(struct msgdata *data, char *Desc, char *Msg);
  37.  
  38.  
  39. MUI_DISPATCH(msg_dispatch)
  40. {
  41.     switch(msg->MethodID) {
  42.         case OM_NEW:
  43.             return(msg_new(cl, obj, (APTR)msg));
  44.         case MSG_OPEN:
  45.             set(obj, MUIA_Window_Open, TRUE);
  46.             return(NULL);
  47.         case MSG_GOT:
  48.             {
  49.             struct msgdata *data = INST_DATA(cl, obj);
  50.             long winopen;
  51.  
  52.             get(obj, MUIA_Window_Open, &winopen);
  53.             if (!winopen) set(obj, MUIA_Window_Open, TRUE);
  54.             /* Open the window if it's not already opened, when receiving a message */
  55.  
  56.             msg_got(data, (char *)((muimsg)msg)->arg1, (char *)((muimsg)msg)->arg2);
  57.             return(NULL);
  58.             }
  59.         case MSG_SAY:
  60.             {
  61.             struct msgdata *data = INST_DATA(cl,obj);
  62.             msg_say(data);
  63.             return(NULL);
  64.             }
  65.         case MSG_WHOIS:
  66.             {
  67.             struct msgdata *data = INST_DATA(cl,obj);
  68.             msg_whois(data, (char *)(((muimsg)msg)->arg1));
  69.             return(NULL);
  70.             }
  71.         case MSG_WHOWAS:
  72.             {
  73.             struct msgdata *data = INST_DATA(cl,obj);
  74.             msg_whowas(data, (char *)(((muimsg)msg)->arg1), (char *)(((muimsg)msg)->arg2), (u_long)(((muimsg)msg)->arg3));
  75.             return(NULL);
  76.             }
  77.  
  78.     }
  79.     return(DoSuperMethodA(cl, obj, msg));
  80. }
  81.  
  82.  
  83. ULONG msg_new(struct IClass *cl, Object *obj, struct opSet *msg)
  84. {
  85.     static struct Hook MessageDispHook = { {NULL, NULL}, &MessageDisplay, NULL, NULL };
  86.  
  87.     struct msgdata *data;
  88.     Object *msglist, *msgstr;
  89.  
  90.     if (obj = (Object *)DoSuperNew(cl, obj,
  91.         MUIA_HelpNode, "messenger",
  92.         WindowContents, VGroup,
  93.             Child, NListviewObject,
  94.                 MUIA_NListview_NList, msglist = NListObject,
  95.                     ReadListFrame,
  96.                     MUIA_NList_Input, FALSE,
  97.                     MUIA_NList_DefaultObjectOnClick, FALSE,
  98.                     MUIA_NList_DisplayHook, &MessageDispHook,
  99.                     MUIA_NList_Format, "BAR,BAR,",
  100.                     MUIA_NList_AutoCopyToClip, TRUE,
  101.                     MUIA_NList_TypeSelect, MUIV_NList_TypeSelect_Char,
  102.                     MUIA_NList_AutoVisible, TRUE,
  103.                 End,
  104.             End,
  105.             Child, msgstr = NewObject(gui->chatline_mcc->mcc_Class, NULL,
  106.                 StringFrame,
  107.                 MUIA_CycleChain, 1,
  108.                 MUIA_String_MaxLen, 256,
  109.             TAG_DONE),
  110.         End,
  111.         TAG_MORE, msg->ops_AttrList))
  112.     {
  113.         data = INST_DATA(cl, obj);
  114.         data->msglist = msglist;
  115.         data->msgstr = msgstr;
  116.  
  117.         set(obj, MUIA_Window_ID, MAKE_ID('M','E','S','G'));
  118.         set(obj, MUIA_Window_Title, MSG_USER_TITLE),
  119.  
  120.         DoMethod(msgstr, MUIM_Notify, MUIA_String_Acknowledge, MUIV_EveryTime, obj,    1, MSG_SAY);
  121.         DoMethod(msgstr, MUIM_Notify, MUIA_String_Acknowledge, MUIV_EveryTime, obj,    3, MUIM_Set, MUIA_Window_ActiveObject, msgstr);
  122.         DoMethod(msgstr, MUIM_Notify, MUIA_String_Acknowledge, MUIV_EveryTime, msgstr, 3, MUIM_Set, MUIA_String_Contents,     NULL);
  123.  
  124.         DoMethod(obj, MUIM_Notify, MUIA_Window_Activate,     TRUE, obj, 3, MUIM_Set, MUIA_Window_ActiveObject, msgstr);
  125.         DoMethod(obj, MUIM_Notify, MUIA_Window_CloseRequest, TRUE, obj, 3, MUIM_Set, MUIA_Window_Open,         FALSE);
  126.  
  127.         return((ULONG)obj);
  128.     }
  129.     return(0);
  130. }
  131.  
  132.  
  133. MUI_LIST_DISP(MessageDisplay, struct ChatMessage *m)
  134. {
  135.     *array++ = m->TStamp;
  136.     *array++ = m->Nick;
  137.     *array = m->Msg;
  138.  
  139.     return 0;
  140. }
  141.  
  142.  
  143. void msg_say(struct msgdata *data)
  144. {
  145.     u_long tmp;
  146.     char *cmd, *args, *nick, *msg;
  147.     char buf[128];
  148.  
  149.     GetAttr(MUIA_String_Contents, data->msgstr, &tmp);
  150.     if (!((char *)tmp) || (((char *)tmp)[0]) == '\0') return;
  151.  
  152.     DoMethod(data->msgstr, CHATLINE_ADD, tmp);
  153.     if (gui_onlinestate < ONLINE) {
  154.         InsertMsgEntry(data, (char *)MSG_MESSAGE_INFO, (char *)MSG_USER_NOTCONN);
  155.         return;
  156.     }
  157.     cmd = strtok((char *)tmp, " ");
  158.     args = strtok(NULL, "");
  159.     if (!cmd) return;
  160.  
  161.     if (stricmp(cmd, "/whois") == 0) {
  162.         if (!args) InsertMsgEntry(data, (char *)MSG_MESSAGE_ERROR, (char *)MSG_MESSAGE_HELP_WHOIS);
  163.         else nap_sendbuf(NAPC_WHOIS, args);
  164.     }
  165.     else if (stricmp(cmd, "/msg") == 0) {
  166.         if (!args) {
  167.             InsertMsgEntry(data, (char *)MSG_MESSAGE_ERROR, (char *)MSG_MESSAGE_HELP_MSG);
  168.             return;
  169.         }
  170.         nick = strtok(args, " ");
  171.         msg = strtok(NULL, "");
  172.         if (!nick || !msg) {
  173.             InsertMsgEntry(data, (char *)MSG_MESSAGE_ERROR, (char *)MSG_MESSAGE_HELP_MSG);
  174.             return;
  175.         }
  176.  
  177.         if (strlen(msg) > 179) {
  178.             InsertMsgEntry(data, (char *)MSG_MESSAGE_ERROR, (char *)MSG_MESSAGE_TOOLONG);
  179.             return;
  180.         }
  181.         else {
  182.             sprintf(nap_buf, "%s %s", nick, msg);
  183.             nap_send(NAPC_PRIVATEMSG);
  184.  
  185.             sprintf(buf, ">%s<", nick);
  186.             InsertMsgEntry(data, buf, msg);
  187.         }
  188.     }
  189.     else {
  190.         sprintf(buf, (char *)MSG_MESSAGE_UNKNOWNCOMMAND, cmd);
  191.         InsertMsgEntry(data, (char *)MSG_MESSAGE_ERROR, buf);
  192.     }
  193. }
  194.  
  195.  
  196. void msg_whois(struct msgdata *data, char *info)
  197. {
  198.     char buf[1024], buf2[32];
  199.     char *nick, *level, *channels, *status, *client;
  200.     int time, shared, downloads, uploads, link;
  201.     struct RexxMsg *m;
  202.     u_long tmp;
  203.  
  204.     nick =      nap_token(&info);
  205.     level =     nap_token(&info);
  206.     time =      nap_ltoken(&info);
  207.     channels =  nap_token(&info);
  208.     status =    nap_token(&info);
  209.     shared =    nap_itoken(&info);
  210.     downloads = nap_itoken(&info);
  211.     uploads =   nap_itoken(&info);
  212.     link =      nap_itoken(&info);
  213.     client =    nap_token(&info);
  214.  
  215.     if (gRexxCommand == WHOIS) {
  216.         GetAttr(MUIA_Application_RexxMsg, gui->app, &tmp);
  217.         m = (struct RexxMsg *)tmp;
  218.  
  219.         sprintf(buf, "%s.NICK", gSTEM);
  220.         SetRexxVar(m, buf, nick, strlen(nick));
  221.  
  222.         sprintf(buf, "%s.LEVEL", gSTEM);
  223.         SetRexxVar(m, buf, level, strlen(level));
  224.  
  225.         sprintf(buf, "%s.TIME", gSTEM);
  226.         sprintf(buf2, "%ld", time);
  227.         SetRexxVar(m, buf, buf2, strlen(buf2));
  228.  
  229.         sprintf(buf, "%s.CHANNELS", gSTEM);
  230.         if (channels != NULL) SetRexxVar(m, buf, channels, strlen(channels));
  231.         else SetRexxVar(m, buf, "", 0);
  232.  
  233.         sprintf(buf, "%s.STATUS", gSTEM);
  234.         SetRexxVar(m, buf, status, strlen(status));
  235.  
  236.         sprintf(buf, "%s.SHARES", gSTEM);
  237.         sprintf(buf2, "%ld", shared);
  238.         SetRexxVar(m, buf, buf2, strlen(buf2));
  239.  
  240.         sprintf(buf, "%s.DOWNLOADS", gSTEM);
  241.         sprintf(buf2, "%ld", downloads);
  242.         SetRexxVar(m, buf, buf2, strlen(buf2));
  243.  
  244.         sprintf(buf, "%s.UPLOADS", gSTEM);
  245.         sprintf(buf2, "%ld", uploads);
  246.         SetRexxVar(m, buf, buf2, strlen(buf2));
  247.  
  248.         sprintf(buf, "%s.LINK", gSTEM);
  249.         sprintf(buf2, "%ld", link);
  250.         SetRexxVar(m, buf, buf2, strlen(buf2));
  251.  
  252.         sprintf(buf, "%s.CLIENT", gSTEM);
  253.         SetRexxVar(m, buf, client, strlen(client));
  254.  
  255.         gRexxCommand = NONE;
  256.     }
  257.     else {
  258.         sprintf(buf, (char *)MSG_MESSAGE_WHOIS1, nick, level, time/360, (time/60)%60, time%60, status);
  259.         InsertMsgEntry(data, (char *)MSG_MESSAGE_WHOIS_DESC, buf);
  260.  
  261.         if (channels != NULL) {
  262.             sprintf(buf, (char *)MSG_MESSAGE_WHOIS2, channels);
  263.             InsertMsgEntry(data, (char *)MSG_MESSAGE_WHOIS_DESC, buf);
  264.         }
  265.  
  266.         sprintf(buf, (char *)MSG_MESSAGE_WHOIS3,
  267.             shared, downloads, uploads);
  268.         InsertMsgEntry(data, (char *)MSG_MESSAGE_WHOIS_DESC, buf);
  269.  
  270.         sprintf(buf, (char *)MSG_MESSAGE_WHOIS4, client, nap_linktype[link]);
  271.         InsertMsgEntry(data, (char *)MSG_MESSAGE_WHOIS_DESC, buf);
  272.     }
  273.  
  274.     set(data->msglist, MUIA_NList_First, MUIV_NList_Active_Bottom);
  275. }
  276.  
  277.  
  278. void msg_whowas(struct msgdata *data, char *user, char *level, u_long lastseen)
  279. {
  280.     struct ClockData *cd;
  281.     char buf[1024], buf2[32];
  282.     struct RexxMsg *m;
  283.     u_long tmp;
  284.  
  285.     lastseen -= (8*365+2)*24*60*60;
  286.     /* Number of seconds between January 1st/0:00, 1970 and January 1st/0:00, 1978 */
  287.  
  288.     if (gRexxCommand == WHOIS) {
  289.         GetAttr(MUIA_Application_RexxMsg, gui->app, &tmp);
  290.         m = (struct RexxMsg *)tmp;
  291.  
  292.         sprintf(buf, "%s.NICK", gSTEM);
  293.         SetRexxVar(m, buf, user, strlen(user));
  294.  
  295.         sprintf(buf, "%s.LEVEL", gSTEM);
  296.         SetRexxVar(m, buf, level, strlen(level));
  297.  
  298.         sprintf(buf, "%s.LASTSEEN", gSTEM);
  299.         sprintf(buf2, "%ld", lastseen);
  300.         SetRexxVar(m, buf, buf2, strlen(buf2));
  301.  
  302.         gRexxCommand = NONE;
  303.         gRC = 1;
  304.     }
  305.     else if (cd = malloc(sizeof(struct ClockData))) {
  306.         Amiga2Date(lastseen, cd);
  307.         sprintf(buf, (char *)MSG_MESSAGE_WHOWAS, user, level, cd->month, cd->mday, cd->year, cd->hour, cd->min, cd->sec);
  308.         free(cd);
  309.         InsertMsgEntry(data, (char *)MSG_MESSAGE_WHOWAS_DESC, buf);
  310.     }
  311. #ifdef AMSTER_DEBUG
  312.     else gui_debug("Out of memory: msg_whowas()");
  313. #endif
  314. }
  315.  
  316.  
  317. void msg_got(struct msgdata *data, char *nick, char *msg)
  318. {
  319.     char buf[32];
  320.  
  321.     prf_event(PRFE_INMSG, nick, msg);
  322.     sprintf(buf, "<%s>", nick);
  323.     InsertMsgEntry(data, buf, msg);
  324. }
  325.  
  326.  
  327. void msg_gotwhois(char *buf)
  328. {
  329.     DoMethod(gui->mwin, MSG_WHOIS, buf);
  330. }
  331.  
  332.  
  333. void InsertMsgEntry(struct msgdata *data, char *Desc, char *Msg)
  334. {
  335.     struct ClockData cd;
  336.     struct ChatMessage *m;
  337.  
  338.     m = malloc(sizeof(struct ChatMessage));
  339.     if (!m) return;
  340.  
  341.     Amiga2Date(GetDateStamp(), &cd);
  342.     sprintf(m->TStamp, "%02d:%02d:%02d", cd.hour, cd.min, cd.sec);
  343.  
  344.     m->Nick = strdup(Desc);
  345.     m->Msg = strdup(Msg);
  346.     DoMethod(data->msglist, MUIM_NList_InsertSingleWrap, m, MUIV_NList_Insert_Bottom, WRAPCOL2, ALIGN_LEFT);
  347.     set(data->msglist, MUIA_NList_First, MUIV_NList_Active_Bottom);
  348. }
  349.